def is_safe(board, row, col):
    # 检查同一列是否有其他皇后
    for i in range(row):
        if board[i] == col:
            return False
    # 检查左上对角线
    for i, j in enumerate(board[:row]):
        if j - i == col - row:
            return False
    # 检查右上对角线
    for i, j in enumerate(board[:row]):
        if j + i == col + row:
            return False
    return True

def solve_eight_queens(n=8):
    def backtrack(row):
        if row == n:
            solutions.append(board[:])
            return
        for col in range(n):
            if is_safe(board, row, col):
                board[row] = col
                backtrack(row + 1)
    solutions = []
    board = [-1] * n
    backtrack(0)
    return solutions

# 打印一种解
solution = solve_eight_queens()
print("solution length:", len(solution))
for row in solution[0]:
    line = ['.'] * 8
    line[row] = 'Q'
    print(''.join(line))